/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.user.platform.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yami.shop.bean.param.GrowthParamConfig;
import com.yami.shop.common.bean.SysConfig;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.Json;
import com.yami.shop.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;


/**
 * 成长值基本配置信息
 * @author lhd
 */
@RestController
@RequestMapping("/user/GrowthConfig")
public class GrowthConfigController {

	@Autowired
	private SysConfigService sysConfigService;


	/**
	 * 配置信息
	 */
	@GetMapping("/info/{id}")
	@PreAuthorize("@pms.hasPermission('user:GrowthConfig:info')")
	public ResponseEntity<GrowthParamConfig> info(@PathVariable("id") String key){
		SysConfig config = sysConfigService.getOne(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getParamKey,key));
		if(Objects.isNull(config)){
			return ResponseEntity.ok().build();
		}
		GrowthParamConfig growthConfigParam = Json.parseObject(config.getParamValue(), GrowthParamConfig.class);
		return ResponseEntity.ok(growthConfigParam);
	}

	/**
	 * 保存配置
	 */
	@SysLog("保存配置")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('user:GrowthConfig:save')")
	public ResponseEntity<Void> save(@RequestBody @Valid GrowthParamConfig growthConfigParam){
		SysConfig config = new SysConfig();

		String paramValue = Json.toJsonString(growthConfigParam);
		config.setParamKey(Constant.GROWTH_CONFIG);
		config.setParamValue(paramValue);
		config.setRemark("成长值获取比例规则");
		if (sysConfigService.count(new LambdaQueryWrapper<SysConfig>()
				.eq(SysConfig::getParamKey, config.getParamKey())) > 0) {
//			throw new YamiShopBindException("系统已存在相同配置的key，无法重复添加,请刷新重试！");
			sysConfigService.update(new LambdaUpdateWrapper<SysConfig>()
					.set(SysConfig::getParamValue,config.getParamValue())
					.set(SysConfig::getRemark,config.getRemark())
					.eq(SysConfig::getParamKey,config.getParamKey()));
		}else {
			sysConfigService.save(config);
		}
		sysConfigService.removeSysConfig(Constant.GROWTH_CONFIG);
		return ResponseEntity.ok().build();
	}

//	/**
//	 * 修改配置
//	 */
//	@SysLog("修改配置")
//	@PutMapping
//	@PreAuthorize("@pms.hasPermission('user:GrowthConfig:update')")
//	public ResponseEntity<Void> update(@RequestBody @Valid GrowthParamConfig growthConfigParam){
//		SysConfig config = new SysConfig();
//		config.setId(growthConfigParam.getId());
//		growthConfigParam.setId(null);
//		config.setRemark("成长值获取比例规则");
//		String paramValue = Json.toJsonString(growthConfigParam);
//		config.setParamValue(paramValue);
//		sysConfigService.updateById(config);
//		sysConfigService.removeSysConfig(Constant.GROWTH_CONFIG);
//		return ResponseEntity.ok().build();
//	}


}
